﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2017/10/26 15:40:41------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class coupons_allotment_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected Model.coupons coupons = null;
        protected Model.orders orders = null;        
        protected Model.gift_pack gift_pack = null;
        protected int hidExpiration_form = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            this.id = DTRequest.GetQueryInt("id");
            if (this.id == 0)
            {
                JscriptMsg("传输参数不正确！", "back");
                return;
            }
            if (!new BLL.coupons_allotment().Exists(this.id))
            {
                JscriptMsg("记录不存在或已被删除！", "back");
                return;
            }

            if (!Page.IsPostBack)
            {
                ChkAdminLevel("common_coupons_allotment", DTEnums.ActionEnum.View.ToString()); //检查权限
                BindCategory();
                BindCoupons();

                ShowInfo(this.id);
            }
        }

        #region 绑定品类=================================
        private void BindCategory()
        {
            DataSet dt = new BLL.article_category().GetList(2, 0, "parent_id=0", "id");
            this.rptList.DataSource = dt;
            this.rptList.DataBind();
        }
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                HiddenField hidId = (HiddenField)e.Item.FindControl("hidId");

                DataSet ds = new BLL.article_category().GetList(2, 0, "parent_id!=0 and class_layer = 2 and class_list like '%," + hidId.Value + ",%'", "id");
                if (Utils.CheckDataSet(ds))
                {
                    RadioButtonList cblActionType = (RadioButtonList)e.Item.FindControl("cblActionType");
                    cblActionType.Items.Clear();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        cblActionType.Items.Add(new ListItem(dr["title"].ToString() + "  ", dr["id"].ToString()));
                    }
                }
            }
        }
        #endregion

        #region 绑定其它优惠券=================================
        private void BindCoupons()
        {
            DataSet ds = new BLL.coupons().GetList(0, "id!=" + id + " and status=0", "id");

            if (Utils.CheckDataSet(ds))
            {
                cblCoupons.Items.Clear();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cblCoupons.Items.Add(new ListItem(dr["title"].ToString() + "  ", dr["id"].ToString()));
                }
            }
        }

        #endregion

        #region 获取到期状态

        public string GetOverStatus(string over_time)
        {
            if (over_time == "")
            {
                return "永不过期";
            }
            else
            {
                if (DateTime.Parse(over_time) < DateTime.Now)
                {
                    return "已过期";
                }
                else
                {
                    TimeSpan ts = DateTime.Parse(over_time) - DateTime.Now;
                    int days = ts.Days;
                    if (days == 0)
                    {
                        return "今天到期";
                    }
                    else
                    {
                        return "还差 " + days + " 天到期";
                    }
                }
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.coupons_allotment bll = new BLL.coupons_allotment();
            Model.coupons_allotment model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            txtContents.Text = model.contents;
            txtStart_time.Text = DateTime.Parse(model.start_time.ToString()).ToString("yyyy-MM-dd");
            txtEnd_time.Text = DateTime.Parse(model.end_time.ToString()).ToString("yyyy-MM-dd");
            txtCompany.Text = new BLL.company().GetValue(model.user_id, "company");
            txtConditions_amount.Text = model.conditions_amount.ToString();
            txtAmount.Text = model.amount.ToString();
            txtAllotment.Text = DateTime.Parse(model.allotment_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            txtOrderNo.Text = model.order_no;
            txtOverName.Text = GetOverStatus(model.over_time);
            txtAdd_time.Text = DateTime.Parse(model.add_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            cblExpiration_form.Text = model.expiration_form.ToString();
            txtExpiration_num.Text = model.expiration_num.ToString();
            rblSuperposition.SelectedValue = model.superposition.ToString();
            hidExpiration_form = model.expiration_form;
            txtImgUrl.Text = model.img_url;
            txtBackcolor.Text = model.backcolor;

            if (model.goods_category_ids != "")
            {
                string[] ids = model.goods_category_ids.Split(',');

                for (int i = 0; i < rptList.Items.Count; i++)
                {
                    RadioButtonList cblActionType = (RadioButtonList)rptList.Items[i].FindControl("cblActionType");
                    for (int n = 0; n < cblActionType.Items.Count; n++)
                    {
                        foreach (string id in ids)
                        {
                            if (id == cblActionType.Items[n].Value)
                            {
                                cblActionType.Items[n].Selected = true;
                            }
                        }
                    }
                }
            }

            if (model.coupon_ids != "")
            {
                string[] ids = model.coupon_ids.Split(',');

                for (int n = 0; n < cblCoupons.Items.Count; n++)
                {
                    foreach (string id in ids)
                    {
                        if (id == cblCoupons.Items[n].Value)
                        {
                            cblCoupons.Items[n].Selected = true;
                        }
                    }
                }
            }

        }
        #endregion
    }
}

